<!doctype html>
<html>
  <head>
    <!-- MathJax -->
    <script type="text/javascript"
      src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
    </script>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title>
      Caffe | Reshape Layer
    </title>

    <link rel="icon" type="image/png" href="/images/caffeine-icon.png">

    <link rel="stylesheet" href="/stylesheets/reset.css">
    <link rel="stylesheet" href="/stylesheets/styles.css">
    <link rel="stylesheet" href="/stylesheets/pygment_trac.css">

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-46255508-1', 'daggerfs.com');
    ga('send', 'pageview');
  </script>
    <div class="wrapper">
      <header>
        <h1 class="header"><a href="/">Caffe</a></h1>
        <p class="header">
          Deep learning framework by <a class="header name" href="http://bair.berkeley.edu/">BAIR</a>
        </p>
        <p class="header">
          Created by
          <br>
          <a class="header name" href="http://daggerfs.com/">Yangqing Jia</a>
          <br>
          Lead Developer
          <br>
          <a class="header name" href="http://imaginarynumber.net/">Evan Shelhamer</a>
        <ul>
          <li>
            <a class="buttons github" href="https://github.com/BVLC/caffe">View On GitHub</a>
          </li>
        </ul>
      </header>
      <section>

      <h1 id="reshape-layer">Reshape Layer</h1>
<ul>
  <li>Layer type: <code class="highlighter-rouge">Reshape</code></li>
  <li><a href="http://caffe.berkeleyvision.org/doxygen/classcaffe_1_1ReshapeLayer.html">Doxygen Documentation</a></li>
  <li>Header: <a href="https://github.com/BVLC/caffe/blob/master/include/caffe/layers/reshape_layer.hpp"><code class="highlighter-rouge">./include/caffe/layers/reshape_layer.hpp</code></a></li>
  <li>
    <p>Implementation: <a href="https://github.com/BVLC/caffe/blob/master/src/caffe/layers/reshape_layer.cpp"><code class="highlighter-rouge">./src/caffe/layers/reshape_layer.cpp</code></a></p>
  </li>
  <li>Input
    <ul>
      <li>a single blob with arbitrary dimensions</li>
    </ul>
  </li>
  <li>Output
    <ul>
      <li>the same blob, with modified dimensions, as specified by <code class="highlighter-rouge">reshape_param</code></li>
    </ul>
  </li>
  <li>
    <p>Sample</p>

    <div class="highlighter-rouge"><pre class="highlight"><code>  layer {
    name: "reshape"
    type: "Reshape"
    bottom: "input"
    top: "output"
    reshape_param {
      shape {
        dim: 0  # copy the dimension from below
        dim: 2
        dim: 3
        dim: -1 # infer it from the other dimensions
      }
    }
  }
</code></pre>
    </div>
  </li>
</ul>

<p>The <code class="highlighter-rouge">Reshape</code> layer can be used to change the dimensions of its input, without changing its data. Just like the <code class="highlighter-rouge">Flatten</code> layer, only the dimensions are changed; no data is copied in the process.</p>

<p>Output dimensions are specified by the <code class="highlighter-rouge">ReshapeParam</code> proto. Positive numbers are used directly, setting the corresponding dimension of the output blob. In addition, two special values are accepted for any of the target dimension values:</p>

<ul>
  <li><strong>0</strong> means “copy the respective dimension of the bottom layer”. That is, if the bottom has 2 as its 1st dimension, the top will have 2 as its 1st dimension as well, given <code class="highlighter-rouge">dim: 0</code> as the 1st target dimension.</li>
  <li><strong>-1</strong> stands for “infer this from the other dimensions”. This behavior is similar to that of -1 in <em>numpy</em>’s or <code class="highlighter-rouge">[]</code> for <em>MATLAB</em>’s reshape: this dimension is calculated to keep the overall element count the same as in the bottom layer. At most one -1 can be used in a reshape operation.</li>
</ul>

<p>As another example, specifying <code class="highlighter-rouge">reshape_param { shape { dim: 0 dim: -1 } }</code> makes the layer behave in exactly the same way as the <code class="highlighter-rouge">Flatten</code> layer.</p>

<h2 id="parameters">Parameters</h2>

<ul>
  <li>Parameters (<code class="highlighter-rouge">ReshapeParameter reshape_param</code>)
    <ul>
      <li>Optional: (also see detailed description below)
        <ul>
          <li><code class="highlighter-rouge">shape</code></li>
        </ul>
      </li>
    </ul>
  </li>
  <li>From <a href="https://github.com/BVLC/caffe/blob/master/src/caffe/proto/caffe.proto"><code class="highlighter-rouge">./src/caffe/proto/caffe.proto</code></a>:</li>
</ul>

<figure class="highlight"><pre><code class="language-protobuf" data-lang="protobuf"><span class="kd">message</span> <span class="nc">ReshapeParameter</span> <span class="p">{</span>
  <span class="c1">// Specify the output dimensions. If some of the dimensions are set to 0,
</span>  <span class="c1">// the corresponding dimension from the bottom layer is used (unchanged).
</span>  <span class="c1">// Exactly one dimension may be set to -1, in which case its value is
</span>  <span class="c1">// inferred from the count of the bottom blob and the remaining dimensions.
</span>  <span class="c1">// For example, suppose we want to reshape a 2D blob "input" with shape 2 x 8:
</span>  <span class="c1">//
</span>  <span class="c1">//   layer {
</span>  <span class="c1">//     type: "Reshape" bottom: "input" top: "output"
</span>  <span class="c1">//     reshape_param { ... }
</span>  <span class="c1">//   }
</span>  <span class="c1">//
</span>  <span class="c1">// If "input" is 2D with shape 2 x 8, then the following reshape_param
</span>  <span class="c1">// specifications are all equivalent, producing a 3D blob "output" with shape
</span>  <span class="c1">// 2 x 2 x 4:
</span>  <span class="c1">//
</span>  <span class="c1">//   reshape_param { shape { dim:  2  dim: 2  dim:  4 } }
</span>  <span class="c1">//   reshape_param { shape { dim:  0  dim: 2  dim:  4 } }
</span>  <span class="c1">//   reshape_param { shape { dim:  0  dim: 2  dim: -1 } }
</span>  <span class="c1">//   reshape_param { shape { dim:  0  dim:-1  dim:  4 } }
</span>  <span class="c1">//
</span>  <span class="k">optional</span> <span class="n">BlobShape</span> <span class="na">shape</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

  <span class="c1">// axis and num_axes control the portion of the bottom blob's shape that are
</span>  <span class="c1">// replaced by (included in) the reshape. By default (axis == 0 and
</span>  <span class="c1">// num_axes == -1), the entire bottom blob shape is included in the reshape,
</span>  <span class="c1">// and hence the shape field must specify the entire output shape.
</span>  <span class="c1">//
</span>  <span class="c1">// axis may be non-zero to retain some portion of the beginning of the input
</span>  <span class="c1">// shape (and may be negative to index from the end; e.g., -1 to begin the
</span>  <span class="c1">// reshape after the last axis, including nothing in the reshape,
</span>  <span class="c1">// -2 to include only the last axis, etc.).
</span>  <span class="c1">//
</span>  <span class="c1">// For example, suppose "input" is a 2D blob with shape 2 x 8.
</span>  <span class="c1">// Then the following ReshapeLayer specifications are all equivalent,
</span>  <span class="c1">// producing a blob "output" with shape 2 x 2 x 4:
</span>  <span class="c1">//
</span>  <span class="c1">//   reshape_param { shape { dim: 2  dim: 2  dim: 4 } }
</span>  <span class="c1">//   reshape_param { shape { dim: 2  dim: 4 } axis:  1 }
</span>  <span class="c1">//   reshape_param { shape { dim: 2  dim: 4 } axis: -3 }
</span>  <span class="c1">//
</span>  <span class="c1">// num_axes specifies the extent of the reshape.
</span>  <span class="c1">// If num_axes &gt;= 0 (and axis &gt;= 0), the reshape will be performed only on
</span>  <span class="c1">// input axes in the range [axis, axis+num_axes].
</span>  <span class="c1">// num_axes may also be -1, the default, to include all remaining axes
</span>  <span class="c1">// (starting from axis).
</span>  <span class="c1">//
</span>  <span class="c1">// For example, suppose "input" is a 2D blob with shape 2 x 8.
</span>  <span class="c1">// Then the following ReshapeLayer specifications are equivalent,
</span>  <span class="c1">// producing a blob "output" with shape 1 x 2 x 8.
</span>  <span class="c1">//
</span>  <span class="c1">//   reshape_param { shape { dim:  1  dim: 2  dim:  8 } }
</span>  <span class="c1">//   reshape_param { shape { dim:  1  dim: 2  }  num_axes: 1 }
</span>  <span class="c1">//   reshape_param { shape { dim:  1  }  num_axes: 0 }
</span>  <span class="c1">//
</span>  <span class="c1">// On the other hand, these would produce output blob shape 2 x 1 x 8:
</span>  <span class="c1">//
</span>  <span class="c1">//   reshape_param { shape { dim: 2  dim: 1  dim: 8  }  }
</span>  <span class="c1">//   reshape_param { shape { dim: 1 }  axis: 1  num_axes: 0 }
</span>  <span class="c1">//
</span>  <span class="k">optional</span> <span class="kt">int32</span> <span class="na">axis</span> <span class="o">=</span> <span class="mi">2</span> <span class="p">[</span><span class="k">default</span> <span class="o">=</span> <span class="mi">0</span><span class="p">];</span>
  <span class="k">optional</span> <span class="kt">int32</span> <span class="na">num_axes</span> <span class="o">=</span> <span class="mi">3</span> <span class="p">[</span><span class="k">default</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">];</span>
<span class="p">}</span></code></pre></figure>



      </section>
  </div>
  </body>
</html>
